package com.hq.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.bus.domain.Check;
import com.hq.sys.eneity.LogInfo;
import com.hq.sys.eneity.LogInfoVo;
import com.hq.sys.mapper.LogInfoMapper;
import com.hq.sys.service.LogInfoService;
import com.hq.sys.util.DataGridView;
import com.hq.sys.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @BelongsPackage: com.hq.sys.service.impl
 * @Author: jiangjunjie
 * @CreateTime: 2024-12-23  16:38
 * @Description: TODO
 * @Version: 1.8
 */
@Service
@Transactional
public class LogInfoServiceImpl implements LogInfoService {
    @Autowired
    private LogInfoMapper logInfoMapper;
    @Override
    public DataGridView queryAllLogInfo(LogInfoVo logInfoVo) {
        Page<Object> page = PageHelper.startPage(logInfoVo.getPage(), logInfoVo.getLimit());
        Weekend<LogInfo> weekend = Weekend.of(LogInfo.class);
        if (Objects.nonNull(logInfoVo.getId())){
            weekend.weekendCriteria().andEqualTo(LogInfo::getId, logInfoVo.getId());
        }
        if (Objects.nonNull(logInfoVo.getStartTime()) && Objects.nonNull(logInfoVo.getEndTime())){
            weekend.weekendCriteria().andBetween(LogInfo::getLogintime, logInfoVo.getStartTime(), logInfoVo.getEndTime());
        }
        if (StrUtil.isNotBlank(logInfoVo.getLoginname())){
            weekend.weekendCriteria().andLike(LogInfo::getLoginname, "%" + logInfoVo.getLoginname() + "%");
        }
        if (StrUtil.isNotBlank(logInfoVo.getLoginip())){
            weekend.weekendCriteria().andLike(LogInfo::getLoginip, "%" + logInfoVo.getLoginip() + "%");
        }
        List<LogInfo> data =logInfoMapper.selectByExample(weekend);
        return new DataGridView(page.getTotal(), data);
    }

    @Override
    public int deleteLogInfo(Integer id) {
        return this.logInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatchLogInfo(Integer[] ids) {
        Arrays.asList(ids).stream().forEach(id->this.logInfoMapper.deleteByPrimaryKey(id));
        return 0;
    }
}
